package com.kcwl.idea.plugins.gateway.config;

import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.util.xmlb.XmlSerializerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;

@State(name = "KcGatewaySettings", storages = @Storage("KcGatewaySettings.xml"))
public class KcGatewaySettings implements PersistentStateComponent<KcGatewaySettings> {

    private String gatewayUrl;

    /**
     * 默认作者
     */
    private String author;

    /**
     * 默认产品
     */
    private String productId;

    public static KcGatewaySettings getInstance() {
        Application application = ApplicationManager.getApplication();
        return application.getComponent(KcGatewaySettings.class);
    }

    public static void storeInstance(@NotNull KcGatewaySettings state) {
        getInstance().loadState(state);
    }

    @Nullable
    @Override
    public KcGatewaySettings getState() {
        return this;
    }

    @Override
    public void loadState(@NotNull KcGatewaySettings state) {
        XmlSerializerUtil.copyBean(state, this);
    }

    public String getGatewayUrl() {
        return gatewayUrl;
    }

    public void setGatewayUrl(String gatewayUrl) {
        this.gatewayUrl = gatewayUrl;
    }


    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof KcGatewaySettings)) {
            return false;
        }
        KcGatewaySettings that = (KcGatewaySettings) o;
        if (!Objects.equals(gatewayUrl, that.getGatewayUrl())) {
            return false;
        }
        return true;
    }
}
